package com.learning.server.util;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReplyFormat {

    public static void main(String[] args) {


        ReplyFormat rf = new ReplyFormat();
        rf.process();

    }

    public class Commenter{
        public int strIndex;
        public String group;
        public String name;
        public String supportCount;
    }

    public void process(){
        String reply = getFile("reply");

        Pattern pt = Pattern.compile("\\s(\\d*)\\n([\\S ]{0,16})\\n");


        Pattern patAuthorReply = Pattern.compile("\\s(\\d*)\\s(作者)\\n");

        Commenter startCommenter = null;
        Commenter endCommenter = null;

        for(;;){
            Matcher match = pt.matcher(reply);
            if(match.find()) {
                startCommenter = new Commenter();
                startCommenter.strIndex = reply.indexOf(match.group());
                startCommenter.group = match.group();
                startCommenter.supportCount = match.group(1);
                startCommenter.name = match.group(2);
            }

            if(match.find()) {
                endCommenter = new Commenter();
                endCommenter.strIndex = reply.substring(startCommenter.strIndex+startCommenter.group.length()).indexOf(match.group())+startCommenter.group.length()+startCommenter.strIndex;
                endCommenter.group = match.group();
                endCommenter.supportCount = match.group(1);
                endCommenter.name = match.group(2);
            }

            String processStr = null;
            if(endCommenter != null){
                processStr = reply.substring(startCommenter.strIndex + startCommenter.group.length(),endCommenter.strIndex);
            }else{
                processStr = reply.substring(startCommenter.strIndex + startCommenter.group.length());
            }

            Matcher matchAuthor = patAuthorReply.matcher(processStr);

            String commenterReply = null;
            String authorReply = null;
            String authorSupportCount = null;
            if(matchAuthor.find()){
                int authorIndex = processStr.indexOf(matchAuthor.group());
                commenterReply = processStr.substring(0,authorIndex);
                authorSupportCount = matchAuthor.group(1);
                authorReply = processStr.substring(authorIndex+matchAuthor.group().length());
            }
            else{
                commenterReply = processStr;
            }

            StringBuilder strBuilder = new StringBuilder();
            strBuilder.append(startCommenter.name);
            if(!startCommenter.supportCount.equals("")){
                strBuilder.append("（").append(startCommenter.supportCount).append("）");
            }
            strBuilder.append("\n\t");
            strBuilder.append(commenterReply);
            if(authorReply != null){
                if(authorSupportCount.equals("")) {
                    strBuilder.append("\n");
                }
                strBuilder.append("作者");
                if(!authorSupportCount.equals("")) {
                    strBuilder.append("（").append(authorSupportCount).append("）");
                }

                strBuilder.append("\n\t");

                strBuilder.append(authorReply);
            }
            strBuilder.append("\n");
            System.out.println(strBuilder.toString());

            startCommenter = endCommenter;
            endCommenter = null;

            if(startCommenter == null){
                break;
            }

            reply = reply.substring(startCommenter.strIndex);


        }

    }

    private String getFile(String fileName) {

        StringBuilder result = new StringBuilder("");

        //Get file from resources folder
        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource(fileName).getFile());

        try (Scanner scanner = new Scanner(file)) {

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                result.append(line).append("\n");
            }

            scanner.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return result.toString();

    }


}
